package process_demo.lock.read_write_lock;

/**
 * @author haomin
 * @date 2022/05/05 20:40
 **/
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Main {
    public static void main(String[] args) {
        ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
        Lock readLock = readWriteLock.readLock();
        Lock writeLock = readWriteLock.writeLock();

        // “写”的角色，请求写锁
        // "只读“角色，请求读锁

        // 读+读 不互斥
        // 读+写 互斥
        // 写+写 互斥
        readLock.lock();        // 读锁已经有了
//        writeLock.lock();       // 写锁锁上
        Thread t = new Thread() {
            @Override
            public void run() {
//                readLock.lock();
                readLock.lock();
                System.out.println("子线程也可以加读锁成功");
            }
        };
        t.start();
    }
}